<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客前台评论控制器，负责，业务流程判断
 */
Class PinglunAction extends Action{


	/**
	 * 博客评论
	 * @return [type] [description]
	 */
	Public function pinglun(){
		$id = G('get.pget');
		$C = M('Public')->blogConfig();
		$FIX = (C('URLREWRITE')) ? C('URLSPACE') : '&';
		$mark = false;
		$user = false;
		$isdel = false;
		$getsession = trim(session('userLogin'));
		if (!empty($getsession)) {
			$mark = true;
			$isdel = true;
			$user = loadfile(C('user').session('username').'.php');
		}
		if ($mark) {
			$talk = ($user['talk'] == 'open') ? true : false;
		} else {
			$talk = true;
		}
		$canpl = ($C['cantalk_pl'] == 'open') ? false : true;
		$this->assign('checksessionuser', $getsession);
		$this->assign('user', $user);
		$this->assign('fix', $FIX);
		$this->assign('mark', $mark);
		$this->assign('talk', $talk);
		$this->assign('id', $id);
		$this->assign('isdel', $isdel);
		$this->assign('canpl', $canpl);
		$this->display('@pinglun/pinglun');
	}

	/**
	 * 评论验证码
	 * @return [type] [description]
	 */
	Public function verify(){
		Image::verify();
	}

	/**
	 * Ajax取评论数据
	 * @return [type] [description]
	 */
	Public function ajaxp(){
		$id = G('get.ppget');
		$C = M('Public')->blogConfig();
		$M = M('Pinglun')->getPunlunData($id);
		$mark = false;
		$user = false;
		$getsession = trim(session('userLogin'));
		if (!empty($getsession)) {
			$mark = true;
			$user = loadfile(C('user').session('username').'.php');
		}
		$canpl = ($C['cantalk_pl'] == 'open') ? false : true;
		$this->assign('checksessionuser', $getsession);
		$this->assign('canpl', $canpl);
		$this->assign('data', $M['data']);
		$this->assign('page', $M['page']);
		$this->assign('user', $user);
		$this->assign('mark', $mark);
		$this->assign('lastpage', $M['lastpage']);
		$this->assign('id', $id);
		$this->display('@pinglun/ajaxp');
	}


	/**
	 * 接收评论数据
	 * @return [type] [description]
	 */
	Public function add(){
		if (ISPOST) {
			$code = G('post.code', '', 'md5');
			if ($code != session('verify')) {
				$this->error('Shit，验证码不对？');
			}
			$data = G('post.');
			$getsession = trim(session('userLogin'));
			if (empty($getsession)) {
				if (empty($data['username']) || empty($data['content']) || empty($data['useremail'])) {
					$this->error('Shit，是不是忘记写点什么？');
				}
				if (!preg_match('/^[\w\-\.]+@[\w\-]+(\.\w+)+$/',$data['useremail'])) {
					$this->error('Shit，E-mail地址出什么问题了？');
				}
				if (!empty($data['url'])) {
					if(!preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $data['url'])){
						$this->error('Shit，网址出什么问题了？');
					}
				}
			}
			if (M('Pinglun')->savepunlun($data)) {
				$reurl = U('show/index', array('id' => $data['id'])).'#comments';
				if ($data['pid']) {
					$this->success('回复评论成功～！', $reurl);
				} else {
					$this->success('评论成功～！', $reurl);
				}
			} else {
				$this->error('Shit，填写的东西中有被禁用词？');
			}
		}
	}
}